# mutt completion
#
# Mutt doesn't have an "addressbook" like Pine, but it has aliases and
# a "query" function to retrieve addresses, so that's what we use here.

have mutt || have muttng && {
_muttaddr()
{
    _muttaliases
    _muttquery

    cur=`_get_cword`
    COMPREPLY=( "${COMPREPLY[@]}" $( compgen -u -- "$cur" ) )

    return 0
}

_muttconffiles()
{
    local file sofar
    local -a newconffiles

    sofar=" $1 "
    shift
    while [[ "$1" ]]; do
        newconffiles=( $(sed -rn 's|^source[[:space:]]+([^[:space:]]+).*$|\1|p' $(eval echo $1) ) )
        for file in "${newconffiles[@]}"; do
        [[ ! "$file" ]] || [[ "${sofar/ ${file} / }" != "$sofar" ]] &&
            continue
        sofar="$sofar $file"
        sofar=" $(eval _muttconffiles \"$sofar\" $file) "
        done
        shift
    done
    echo $sofar
}

_muttaliases()
{
    local cur muttrc muttcmd=${COMP_WORDS[0]}
    local -a conffiles aliases
    cur=`_get_cword =`

    [ -f ~/.${muttcmd}/${muttcmd}rc ] && muttrc="~/.${muttcmd}/${muttcmd}rc"
    [ -f ~/.${muttcmd}rc ] && muttrc="~/.${muttcmd}rc"
    [ -z "$muttrc" ] && return 0

    conffiles=( $(eval _muttconffiles $muttrc $muttrc) )
    aliases=( $( sed -rn 's|^alias[[:space:]]+([^[:space:]]+).*$|\1|p' \
        $(eval echo "${conffiles[@]}") ) )
    COMPREPLY=( "${COMPREPLY[@]}" $( compgen -W "${aliases[*]}" -- "$cur" ) )

    return 0
}

_muttquery()
{
    local cur querycmd muttcmd=${COMP_WORDS[0]}
    local -a queryresults
    cur=`_get_cword`

    querycmd="$( $muttcmd -Q query_command  | sed -r 's|^query_command=\"(.*)\"$|\1|; s|%s|'$cur'|' )"
    if [ -z "$cur" -o -z "$querycmd" ]; then
        queryresults=()
    else
        queryresults=( $( $querycmd | sed -nr '2,$s|^([^[:space:]]+).*|\1|p' ) )
    fi

    COMPREPLY=( "${COMPREPLY[@]}" $( compgen -W "${queryresults[*]}" \
        -- "$cur" ) )

    return 0
}

_muttfiledir()
{
    local cur folder spoolfile muttcmd=${COMP_WORDS[0]}
    cur=`_get_cword`

    # This is currently not working so well. Perhaps this function should
    # just call _filedir() for the moment.
    if [[ $cur == [=+]* ]]; then
        folder="$( $muttcmd -Q folder | sed -r 's|^folder=\"(.*)\"$|\1|' )"
        : folder:=~/Mail

        # Match any file in $folder beginning with $cur
        # (minus the leading '=' sign).
        COMPREPLY=( $( compgen -f -- "$folder/${cur:1}" ) )
        COMPREPLY=( ${COMPREPLY[@]#$folder/} )
        return 0
    elif [ "$cur" == !* ]; then
        spoolfile="$( $muttcmd -Q spoolfile | \
            sed -r 's|^spoolfile=\"(.*)\"$|\1|' )"
        [ ! -z "$spoolfile" ] && eval cur="${cur/^!/$spoolfile}";
    fi
    _filedir

    return 0
}

_mutt()
{
    local cur prev
    cur=`_get_cword`
    prev=${COMP_WORDS[COMP_CWORD-1]}

    COMPREPLY=()

    case "$cur" in
    -*)
        COMPREPLY=( $( compgen -W '-A -a -b -c -e -f -F -H -i -m -n \
            -p -Q -R -s -v -x -y -z -Z -h' -- "$cur" ) )
        return 0
        ;;
    *)
        case "$prev" in
        -@(a|f|F|H|i))
            _muttfiledir
            return 0
            ;;
        -A)
            _muttaliases
            return 0
            ;;
        -@(e|m|Q|s|h|p|R|v|y|z|Z))
            return 0
            ;;
        *)
            _muttaddr
            return 0
            ;;
        esac
        ;;
    esac

}
complete -F _mutt $default $filenames mutt muttng
}

# Local variables:
# mode: shell-script
# sh-basic-offset: 4
# sh-indent-comment: t
# indent-tabs-mode: nil
# End:
# ex: ts=4 sw=4 et filetype=sh
